/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import simplemetrosystem.control.ConfigureManager;
import simplemetrosystem.model.charlanguage.NameMap;
import simplemetrosystem.view.Layout.GBC;

/**
 *
 * @author Eric Qian
 */
public class ConfigureFrame extends JFrame {

    /**
     * 
     * @param frame
     */
    public ConfigureFrame(MetroFrame frame) {
        super();
        this.frame = frame;

        try {
            BufferedImage icon = ImageIO.read(new File("icon/configure.gif"));
            this.setIconImage(icon);
        } catch (IOException ex) {
            Logger.getLogger(ConfigureFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

        cFrame = this;
        nameMap = NameMap.getInstance();
        configureManager = ConfigureManager.getInstance();

        language = configureManager.getLanguage();
        undoStep = configureManager.getUndoStep();
        mincost = configureManager.getMinCost();
        maxcost = configureManager.getMaxCost();
        minkm = configureManager.getMinkm();
        perkm = configureManager.getPerkmcost();
        virchange = configureManager.getVirchange();
        width = configureManager.getWidth();
        height = configureManager.getHeight();

        this.setTitle(nameMap.getName("Configure"));
        //用户窗体大小可调
        this.setResizable(false);
        //加载所有窗体组件
        loadComponents();
        pack();

        Toolkit theKit = this.getToolkit(); // Get the window toolkit

        Dimension wndSize = theKit.getScreenSize(); // Get screen size

        //设置窗体默认大小
        this.setBounds((int) (wndSize.getWidth() - FRAME_WIDTH) / 2,
                (int) (wndSize.getHeight() - FRAME_HEIGHT) / 2, FRAME_WIDTH, FRAME_HEIGHT);
        this.setVisible(true);
    }

    private void loadComponents() {
        this.setLayout(new GridBagLayout());

        JTabbedPane pane = new JTabbedPane();
        add(pane, new GBC(0, 0, 2, 1).setWeight(1, 1).setFill(GBC.BOTH));

        pane.addTab(nameMap.getName("Cost"), this.getCostPane());
        pane.addTab(nameMap.getName("Canvas"), this.getCanvasPane());
        pane.addTab(nameMap.getName("Undo"), this.getUndoPane());
        pane.addTab(nameMap.getName("Language"), this.getLanguagePane());

        add(getSaveButton(), new GBC(0, 1).setWeight(1, 0).setAnchor(GBC.EAST).setInsets(4, 10, 4, 10));
        add(getCloseButton(), new GBC(1, 1).setWeight(0, 0).setAnchor(GBC.CENTER).setInsets(4, 10, 4, 10));
    }

    public JPanel getCanvasPane() {
        if (canvasPane == null) {
            canvasPane = new JPanel();
            canvasPane.setLayout(new GridBagLayout());
            canvasPane.add(new JLabel(nameMap.getName("Canvas Width")),
                    new GBC(0, 0).setWeight(1, 1).setInset(10).setAnchor(GBC.EAST));
            canvasPane.add(new JLabel(nameMap.getName("Canvas Height")),
                    new GBC(0, 1).setWeight(1, 1).setInset(10).setAnchor(GBC.EAST));
            cWField = new JTextField();
            cWField.setText("" + width);
            canvasPane.add(cWField, new GBC(1, 0).setWeight(5, 0).setFill(GBC.HORIZONTAL));
            cHField = new JTextField();
            cHField.setText("" + height);
            canvasPane.add(cHField, new GBC(1, 1).setWeight(5, 0).setFill(GBC.HORIZONTAL));
        }
        return canvasPane;
    }

    public JPanel getCostPane() {
        if (costPane == null) {
            costPane = new JPanel();
            costPane.setLayout(new GridBagLayout());
            costPane.add(new JLabel(nameMap.getName("Min Fare")),
                    new GBC(0, 0).setWeight(1, 1).setAnchor(GBC.EAST));
            costPane.add(new JLabel(nameMap.getName("Max Fare")),
                    new GBC(0, 1).setWeight(1, 1).setAnchor(GBC.EAST));
            costPane.add(new JLabel(nameMap.getName("One Kilometer Cost")),
                    new GBC(0, 2).setWeight(1, 1).setAnchor(GBC.EAST));
            costPane.add(new JLabel(nameMap.getName("Min Kilometer")),
                    new GBC(0, 3).setWeight(1, 1).setAnchor(GBC.EAST));
            costPane.add(new JLabel(nameMap.getName("Virtual Change Speical Offer")),
                    new GBC(0, 4).setWeight(1, 1).setAnchor(GBC.EAST));

            minFField = new JTextField();
            minFField.setText("" + mincost);
            maxFField = new JTextField();
            maxFField.setText("" + maxcost);
            okcField = new JTextField();
            okcField.setText("" + perkm);
            mkField = new JTextField();
            mkField.setText("" + minkm);
            vcsoField = new JTextField();
            vcsoField.setText("" + virchange);

            costPane.add(minFField,
                    new GBC(1, 0).setWeight(5, 1).setFill(GBC.HORIZONTAL).setAnchor(GBC.WEST).setInset(10));
            costPane.add(maxFField,
                    new GBC(1, 1).setWeight(5, 1).setFill(GBC.HORIZONTAL).setAnchor(GBC.WEST).setInset(10));
            costPane.add(okcField,
                    new GBC(1, 2).setWeight(5, 1).setFill(GBC.HORIZONTAL).setAnchor(GBC.WEST).setInset(10));
            costPane.add(mkField,
                    new GBC(1, 3).setWeight(5, 1).setFill(GBC.HORIZONTAL).setAnchor(GBC.WEST).setInset(10));
            costPane.add(vcsoField,
                    new GBC(1, 4).setWeight(5, 1).setFill(GBC.HORIZONTAL).setAnchor(GBC.WEST).setInset(10));
        }
        return costPane;
    }

    public JPanel getLanguagePane() {
        if (languagePane == null) {
            languagePane = new JPanel();
            languagePane.setLayout(new GridBagLayout());
            languagePane.add(new JLabel(nameMap.getName("Language") + ": "),
                    new GBC(0, 0).setInset(10).setWeight(1, 1).setAnchor(GBC.EAST));
            languageBox = new JComboBox();
            for (int i = 0; i < nameMap.getLangagesSize(); i++) {
                languageBox.addItem(nameMap.getLanguageAt(i));
            }
            languageBox.setSelectedItem(language);
            languageBox.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    language = nameMap.getLanguageAt(languageBox.getSelectedIndex());
                }
            });
            languagePane.add(languageBox,
                    new GBC(1, 0).setAnchor(GBC.CENTER).setWeight(1, 1));
        }
        return languagePane;
    }

    public JPanel getUndoPane() {
        if (undoPane == null) {
            undoPane = new JPanel();
            undoPane.setLayout(new GridBagLayout());
            undoPane.add(new JLabel(nameMap.getName("Undo step")),
                    new GBC(0, 0).setInset(10).setWeight(1, 1).setAnchor(GBC.EAST));
            undoField = new JTextField();
            undoField.setText("" + undoStep);
            undoPane.add(undoField, new GBC(1, 0).setWeight(5, 0).setFill(GBC.HORIZONTAL));
        }
        return undoPane;
    }

    public JButton getSaveButton() {
        if (saveButton == null) {
            saveButton = new JButton();
            saveButton.setText(nameMap.getName("Save"));
            saveButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    try {
                        height = Integer.parseInt(cHField.getText());
                        width = Integer.parseInt(cWField.getText());
                        maxcost = Integer.parseInt(maxFField.getText());
                        mincost = Integer.parseInt(minFField.getText());
                        minkm = Double.parseDouble(mkField.getText());
                        perkm = Double.parseDouble(okcField.getText());
                        virchange = Integer.parseInt(vcsoField.getText());
                        undoStep = Integer.parseInt(undoField.getText());
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(cFrame,
                                nameMap.getName("Input Error"),
                                nameMap.getName("WARNING"), JOptionPane.WARNING_MESSAGE);
                    }

                    configureManager.setLanguage(language);
                    configureManager.setHeight(height);
                    configureManager.setWidth(width);
                    configureManager.setMaxCost(maxcost);
                    configureManager.setMinCost(mincost);
                    configureManager.setMinkm(minkm);
                    configureManager.setPerkmcost(perkm);
                    configureManager.setVirchange(virchange);
                    configureManager.setUndoStep(undoStep);
                    configureManager.saveConfigure();
                    cFrame.setVisible(false);
                }
            });

        }
        return saveButton;
    }

    public JButton getCloseButton() {
        if (closeButton == null) {
            closeButton = new JButton();
            closeButton.setText(nameMap.getName("Close"));
            closeButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    cFrame.setVisible(false);
                }
            });
        }
        return closeButton;
    }
    private ConfigureFrame cFrame;
    private MetroFrame frame;
    private NameMap nameMap;
    private ConfigureManager configureManager;
    private JPanel languagePane;
    private JPanel canvasPane;
    private JPanel undoPane;
    private JPanel costPane;
    private JComboBox languageBox;
    private JButton saveButton;
    private JButton closeButton;
    private JTextField minFField;
    private JTextField maxFField;
    private JTextField okcField;
    private JTextField mkField;
    private JTextField vcsoField;
    private JTextField undoField;
    private JTextField cWField;
    private JTextField cHField;
    private String language;
    private int undoStep;
    private int width;
    private int height;
    private int mincost;
    private int maxcost;
    private double minkm;
    private double perkm;
    private int virchange;
    public static final int FRAME_WIDTH = 400;
    public static final int FRAME_HEIGHT = 300;
}